﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.HtmlControls;

namespace Department.admin.SiteStructure
{
    public partial class ChangeCSS : System.Web.UI.Page
    {
        const int MAX_FILE_SIZE = AdminVariablesSingleton.MAX_FILE_SIZE;
        DirectoryInfo cssFiles;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Смяна на CSS";

            AdminVariablesSingleton.changeCSS(Master, "struct");

            Control foundControl = AdminVariablesSingleton.FindControlRecursive(Master, "viewCSS");
            ((HtmlControl)foundControl).Attributes.Add("class", "sub_show");

            cssFiles = new DirectoryInfo(Server.MapPath("~/CSS/uploadedCSS/"));
            if (!IsPostBack)
            {
                cssList.DataSource = cssFiles.GetFiles("*.css");
                cssList.DataBind();
            }

        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                string selectedCSS = cssList.Text;
                string sourceFile = Server.MapPath("~/CSS/uploadedCSS/" + selectedCSS);
                string destinationFile = Server.MapPath("~/CSS/style.css");
                System.IO.File.Delete(destinationFile);
                System.IO.File.Copy(sourceFile, destinationFile);
            }
            catch (Exception ex)
            {
                Response.Redirect("/admin/ErrorPage.aspx?type=1&err=" + ex.Message);
            }
        }



        protected void uploadCSS_Click(object sender, EventArgs e)
        {
            if (cssUpload.HasFile)
            {
                try
                {
                    if (cssUpload.PostedFile.ContentLength < MAX_FILE_SIZE)
                    {

                        string filename = Path.GetFileName(cssUpload.FileName);
                        filename = DateTime.Now.ToUniversalTime().ToFileTimeUtc() + filename;

                        cssUpload.SaveAs(Server.MapPath("~/CSS/uploadedCSS/") + filename);

                        cssList.DataSource = cssFiles.GetFiles("*.css");
                        cssList.DataBind();

                    }
                }
                catch (Exception ex)
                {
                    Response.Redirect("/admin/ErrorPage.aspx?type=1&err=" + ex.Message);
                }
            }
        }
    }
}